// pages/blueconn/blueconn.js
var app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        list: [],
        services: [],
        serviceId: 0,
        writeCharacter: false,
        readCharacter: false,
        notifyCharacter: false,
        isScanning: false
    },
    //搜索设备
    startSearch: function () {
        var that = this
        wx.closeBluetoothAdapter({
            success(res) {
                console.log(res)
            }
        })
        wx.openBluetoothAdapter({
            success: function (res) {
                wx.getBluetoothAdapterState({
                    success: function (res) {
                        console.log('openBluetoothAdapter success', res)
                        if (res.available) {
                            if (res.discovering) {
                                wx.stopBluetoothDevicesDiscovery({
                                    success: function (res) {
                                        console.log(res)
                                    }
                                })
                            } else {
                                // that.startBluetoothDevicesDiscovery()
                                that.getBluetoothDevices()
                            }
                            // that.checkPemission()
                        } else {
                            wx.showModal({
                                title: '提示',
                                content: '本机蓝牙不可用',
                                showCancel: false
                            })
                        }
                    },
                })
            }, fail: function (e) {
                // if (res.errCode === 10001) {
                //   wx.onBluetoothAdapterStateChange(function (res) {
                //     console.log('onBluetoothAdapterStateChange', res)
                //     if (res.available) {
                //       this.startBluetoothDevicesDiscovery()
                //     }
                //   })
                // }
                console.log("蓝牙初始化失败")
                console.log(e)
                wx.showModal({
                    title: '提示',
                    content: '蓝牙初始化失败，请到设置打开蓝牙',
                    showCancel: false
                })
            }
        })
    },
    // 检查地理位置权限
    checkPemission: function () {  //android 6.0以上需授权地理位置权限
        var that = this
        var platform = app.BLEInformation.platform
        if (platform == "ios") {
            app.globalData.platform = "ios"
            that.getBluetoothDevices()
        } else if (platform == "android") {
            app.globalData.platform = "android"
            console.log(app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8), app.getSystem().length - (app.getSystem().length - 8) + 1))
            if (app.getSystem().substring(app.getSystem().length - (app.getSystem().length - 8), app.getSystem().length - (app.getSystem().length - 8) + 1) > 5) {
                wx.getSetting({
                    success: function (res) {
                        console.log(res)
                        if (!res.authSetting['scope.userLocation']) {
                            wx.authorize({
                                scope: 'scope.userLocation',
                                complete: function (res) {
                                    that.getBluetoothDevices()
                                }
                            })
                        } else {
                            that.getBluetoothDevices()
                        }
                    }
                })
            }
        }
    },
    // 获取蓝牙设备列表
    getBluetoothDevices: function () {  //获取蓝牙设备信息
        var that = this
        console.log("start search")
        wx.showLoading({
            title: '正在加载',
            icon: 'loading',
        })
        that.setData({
            isScanning: true
        })
        wx.startBluetoothDevicesDiscovery({
            success: function (res) {
                console.log(res)
                setTimeout(function () {
                    wx.getBluetoothDevices({
                        success: function (res) {
                            console.log("getBluetoothDevices")
                            console.log(res)
                            var devices = []
                            var num = 0
                            for (var i = 0; i < res.devices.length; ++i) {
                                if (res.devices[i].name != "未知设备") {
                                    devices[num] = res.devices[i]
                                    num++
                                }
                            }

                            that.setData({
                                list: devices,
                                isScanning: false
                            })
                            wx.hideLoading()
                            wx.stopPullDownRefresh()
                            wx.stopBluetoothDevicesDiscovery({
                                success: function (res) {
                                    console.log("停止搜索蓝牙")
                                }
                            })

                        },
                    })
                }, 1000)
            },
        })
    },

    ab2hex: function (buffer) {
        var hexArr = Array.prototype.map.call(
            new Uint8Array(buffer),
            function (bit) {
                return ('00' + bit.toString(16)).slice(-2)
            }
        )
        return hexArr.join('');
    },
    // 点击蓝牙设备连接
    bindViewTap: function (e) {
        var that = this
        wx.stopBluetoothDevicesDiscovery({
            success: function (res) {
                console.log(res)
            },
        })
        that.setData({
            serviceId: 0,
            writeCharacter: false,
            readCharacter: false,
            notifyCharacter: false
        })
        console.log(e.currentTarget.dataset.title)
        wx.showLoading({
            title: '正在连接',
        })
        wx.createBLEConnection({
            deviceId: e.currentTarget.dataset.title,
            success: function (res) {
                console.log(res)
                app.BLEInformation.deviceId = e.currentTarget.dataset.title
                that.getSeviceId()
            }, fail: function (e) {
                wx.showModal({
                    title: '提示',
                    content: '连接失败',
                    showCancel: false
                })
                console.log(e)
                wx.hideLoading()
            }, complete: function (e) {
                console.log(e)
            }
        })
    },
    // 获取设备服务列表
    getSeviceId: function () {
        var that = this
        var platform = app.BLEInformation.platform
        console.log(app.BLEInformation.deviceId)
        wx.getBLEDeviceServices({
            deviceId: app.BLEInformation.deviceId,
            success: function (res) {
                console.log(res)
                // var realId = ''
                // if (platform == 'android') {
                //   // for(var i=0;i<res.services.length;++i){
                //   // var item = res.services[i].uuid
                //   // if (item == "0000FEE7-0000-1000-8000-00805F9B34FB"){
                //   realId = "0000FEE7-0000-1000-8000-00805F9B34FB"
                //   //       break;
                //   //     }
                //   // }
                // } else if (platform == 'ios') {
                //   // for(var i=0;i<res.services.length;++i){
                //   // var item = res.services[i].uuid
                //   // if (item == "49535343-FE7D-4AE5-8FA9-9FAFD205E455"){
                //   realId = "49535343-FE7D-4AE5-8FA9-9FAFD205E455"
                //   // break
                //   // }
                //   // }
                // }
                // app.BLEInformation.serviceId = realId
                that.setData({
                    services: res.services
                })
                that.getCharacteristics()
            }, fail: function (e) {
                console.log(e)
            }, complete: function (e) {
                console.log(e)
            }
        })
    },
    // 获取蓝牙低功耗设备某个服务中所有特征
    getCharacteristics: function () {
        var that = this
        var list = that.data.services
        var num = that.data.serviceId
        var write = that.data.writeCharacter
        var read = that.data.readCharacter
        var notify = that.data.notifyCharacter
        wx.getBLEDeviceCharacteristics({
            deviceId: app.BLEInformation.deviceId,
            serviceId: list[num].uuid,
            success: function (res) {
                console.log(res)
                for (var i = 0; i < res.characteristics.length; ++i) {
                    var properties = res.characteristics[i].properties
                    var item = res.characteristics[i].uuid
                    if (!notify) {
                        if (properties.notify) {
                            app.BLEInformation.notifyCharaterId = item
                            app.BLEInformation.notifyServiceId = list[num].uuid
                            notify = true
                        }
                    }
                    if (!write) {
                        if (properties.write) {
                            app.BLEInformation.writeCharaterId = item
                            app.BLEInformation.writeServiceId = list[num].uuid
                            write = true
                        }
                    }
                    if (!read) {
                        if (properties.read) {
                            app.BLEInformation.readCharaterId = item
                            app.BLEInformation.readServiceId = list[num].uuid
                            read = true
                        }
                    }
                }
                if (!write || !notify || !read) {
                    num++
                    that.setData({
                        writeCharacter: write,
                        readCharacter: read,
                        notifyCharacter: notify,
                        serviceId: num
                    })
                    if (num == list.length) {
                        wx.showModal({
                            title: '提示',
                            content: '找不到该读写的特征值',
                            showCancel: false
                        })
                    } else {
                        that.getCharacteristics()
                    }
                } else {
                    wx.showToast({
                        title: '连接成功',
                    })
                    let printer = {}
                    printer.osType = app.getModel()
                    printer.osVersion = app.getSystem()
                    printer.deviceId = app.BLEInformation.deviceId
                    printer.notifyCharaterId = app.BLEInformation.notifyCharaterId
                    printer.notifyServiceId = app.BLEInformation.notifyServiceId
                    printer.writeCharaterId = app.BLEInformation.writeCharaterId
                    printer.writeServiceId = app.BLEInformation.writeServiceId
                    printer.readCharaterId = app.BLEInformation.readCharaterId
                    printer.readServiceId = app.BLEInformation.readServiceId
                    app.formPost('/app/boss/printer/update', printer).then(res => {
                        if (res.code === 0) {

                        } else {
                            app.message(res.message, 'error')
                        }
                    }).catch(e => {
                        _this.setData({
                            spinShow: false
                        });
                        app.message(e, 'error')
                    })
                    that.openControl()
                }
            }, fail: function (e) {
                console.log(e)
            }, complete: function (e) {
                console.log("write:" + app.BLEInformation.writeCharaterId)
                console.log("read:" + app.BLEInformation.readCharaterId)
                console.log("notify:" + app.BLEInformation.notifyCharaterId)
            }
        })
    },
    openControl: function () {//连接成功返回主页
        app.globalData.isConnBLE = true
        setTimeout(function () {
            wx.navigateBack()
        }, 2000)

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        app.BLEInformation.platform = app.getPlatform()
        this.startSearch()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

})
